//
//  FindContenView.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/4.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "FindContenView.h"

@implementation FindContenView

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        
        
        /**
         加入书架
         */
        
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setTitle:@"加入书架" forState:UIControlStateNormal];
        [addBtn setTitleColor:CSColorFromRGB(0xF52C50) forState:UIControlStateNormal];
        addBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        addBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        addBtn.imageView.layer.masksToBounds = YES;
        addBtn.layer.cornerRadius = 5;
        addBtn.layer.borderWidth = 1;
        addBtn.layer.borderColor = CSColorFromRGB(0xF52C50).CGColor;
        addBtn.clipsToBounds = YES;
        [self addSubview:addBtn];
        self.addBtn = addBtn;
        
        
        /**
         开始阅读
         */
        UIView *sv = [[UIView alloc]init];
        self.startBgView = sv;
        sv.layer.cornerRadius = 5;
        
        sv.clipsToBounds = YES;
        [self addSubview:sv];
        
        UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [startBtn setTitle:@"开始阅读" forState:UIControlStateNormal];
        [startBtn setTitleColor:CSWhiteColor forState:UIControlStateNormal];
        startBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        startBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        startBtn.imageView.layer.masksToBounds = YES;
        startBtn.layer.cornerRadius = 5;
        startBtn.layer.borderWidth = 1;
        startBtn.layer.borderColor = CSColorFromRGB(0xF52C50).CGColor;
        startBtn.clipsToBounds = YES;
        [self addSubview:startBtn];
        self.startBtn = startBtn;
        
        /**
         分割线
         */
        UIView *sepView = [[UIView alloc]init];
        sepView.backgroundColor = CSColorFromRGB(0xF5F5F5);
        self.sepView = sepView;
        [self addSubview:sepView];
        
        /**
         阅读次数
         */
        
        UILabel *readCountLable = [[UILabel alloc]init];
        readCountLable.text = @"阅读 2.72w";
        readCountLable.textColor = CSColorFromRGB(0x6B6763);
        readCountLable.font = [UIFont systemFontOfSize:14];
        self.readCountLable = readCountLable;
        [self addSubview:readCountLable];
        
        /**
         赞数
         */
        
        UIButton *zanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [zanBtn setImage:[UIImage imageNamed:@"点赞"] forState:UIControlStateNormal];
        [zanBtn setImage:[UIImage imageNamed:@"已点赞拷贝"] forState:UIControlStateSelected];
        [zanBtn setTitle:@"2800" forState:UIControlStateNormal];
        [zanBtn setTitleColor:CSColorFromRGB(0x6B6763) forState:UIControlStateNormal];
        zanBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [self addSubview:zanBtn];
        self.zanBtn = zanBtn;
        
        /**
         广告
         */
        UIButton *advertisBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [advertisBtn setImage:[UIImage imageNamed:@"底部广告"] forState:UIControlStateNormal];
        advertisBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        self.advertisBtn = advertisBtn;
        [self addSubview:advertisBtn];
        
        /**
         广告
         */
        UILabel *advertislLable = [[UILabel alloc]init];
        advertislLable.text = @"广告";
        advertislLable.textColor = CSWhiteColor;
        advertislLable.layer.cornerRadius = 5;
        advertislLable.clipsToBounds = YES;
        advertislLable.textAlignment = NSTextAlignmentCenter;
        advertislLable.font = [UIFont systemFontOfSize:12];
        advertislLable.backgroundColor = [CSBlackColor1 colorWithAlphaComponent:0.3];
        self.advertislLable = advertislLable;
        [self addSubview:advertislLable];
        
        [self setConstraints];
        
    }
    return self;
}

-(void)setConstraints
{
    //设置约束条件
    
    /**
     加入书架
     */
    CGFloat w = (SCREENWIDTH - 90)/2;
    [self.addBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(20);
        make.left.mas_equalTo(self).offset(30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(w);
        
    }];
    
    /**
     开始阅读
     */
    [self.startBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(w);
        
    }];
    [self.startBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).offset(20);
        make.right.mas_equalTo(self).offset(-30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(w);
        
    }];
    
    
    /**
     分割线
     */
    [self.sepView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.addBtn.mas_bottom).offset(25);
        
    }];
    
    
    /**
     阅读次数
     */
    [self.readCountLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sepView.mas_bottom).offset(24);
        make.left.mas_equalTo(self).offset(12);
        make.height.mas_equalTo(20);
        
    }];
    
    /**
     赞数
     */
    
    [self.zanBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.sepView.mas_bottom).offset(24);
        make.right.mas_equalTo(self).offset(-13);
        make.height.mas_equalTo(20);
        
    }];
    
    
    
    /**
     广告
     */
    [self.advertisBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.readCountLable.mas_bottom).offset(20);
        make.right.mas_equalTo(self).offset(-13);
        make.left.mas_equalTo(self).offset(12);
        make.height.mas_equalTo(90);
        make.bottom.mas_equalTo(self).offset(-15);
        
    }];
    
    /**
     广告
     */
    [self.advertislLable mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.advertisBtn).offset(7);
        make.right.mas_equalTo(self.advertisBtn).offset(-7);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
        
    }];
}

-(void)layoutSubviews
{
    
    [self.zanBtn imageOnTheTitleLeftWithSpace:7];
    
    
}

-(void)drawRect:(CGRect)rect
{
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors =  @[(id)CSColorFromRGB(0xF52C50).CGColor,(id)CSColorFromRGB(0xF95C48).CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = self.startBgView.bounds;
    [self.startBgView.layer insertSublayer:gradientLayer atIndex:0];
    

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}




@end

@implementation FindContenToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        /**
         分割线
         */
        UIView *sepView = [[UIView alloc]init];
        sepView.backgroundColor = CSColorFromRGB(0xF5F5F5);
        self.sepView = sepView;
        [self addSubview:sepView];
        
        /**
         输入
         */
        
        UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [writeBtn setTitle:@"发表评论"];
        [writeBtn setTitleColor:CSColorFromRGB(0x6B6763)];
        writeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        writeBtn.layer.borderWidth = 1;
        writeBtn.layer.borderColor = CSColorFromRGB(0xDCDCDC).CGColor;
        writeBtn.layer.cornerRadius = 5;
        writeBtn.clipsToBounds = YES;
        writeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        writeBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
        [self addSubview:writeBtn];
        self.writeBtn = writeBtn;
        
        
        /**
         评论
         */
        
        
        UIButton *comBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [comBtn setImage:[UIImage imageNamed:@"评论"] forState:UIControlStateNormal];
        comBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [self addSubview:comBtn];
        self.comBtn = comBtn;
        
        /**
         评论数
         */
        UIButton *comCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        comCountBtn.backgroundColor = CSColorFromRGB(0xF95C48);
        comCountBtn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
        comCountBtn.titleLabel.font = SmallFont;
        [comCountBtn setTitle:@"99+" forState:UIControlStateNormal];
        [comCountBtn setTitleColor:CSWhiteColor forState:UIControlStateNormal];
        comCountBtn.layer.cornerRadius = 8;
        comCountBtn.clipsToBounds = YES;
        [self addSubview:comCountBtn];
        self.comCountBtn = comCountBtn;
        
        /**
         分享
         */
        UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [shareBtn setImage:[UIImage imageNamed:@"转发"] forState:UIControlStateNormal];
        shareBtn.contentEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
        [self addSubview:shareBtn];
        self.shareBtn = shareBtn;
        
        [self setConstraints];
        
    }
    return self;
    
}

-(void)setConstraints
{
    //设置约束条件
    
    /**
     分割线
     */
    [self.sepView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self);
        
    }];
    
    /**
     输入
     */
    [self.writeBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self).offset(12);
        make.centerY.mas_equalTo(self).offset(-TabBarHeightIphonexSafe/2);
        make.height.mas_equalTo(33);
        make.right.mas_equalTo(self).offset(-SCREENWIDTH/375*116 );
        
    }];
    
    /**
     评论
     */
    [self.comBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self).offset(-TabBarHeightIphonexSafe/2);
        
    }];
    
    /**
     评论数
     */
    [self.comCountBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.comBtn).offset(2);
        make.centerX.mas_equalTo(self.comBtn.mas_right).offset(-10);
        make.height.mas_equalTo(16);
        
    }];
    
    /**
     分享
     */
    [self.shareBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.comBtn.mas_right).offset(15);
        make.centerY.mas_equalTo(self).offset(-TabBarHeightIphonexSafe/2);
        make.right.mas_equalTo(self).offset(-3);
        
    }];
    
}

@end

@interface FindCommentToolView ()<UITextViewDelegate,TTTAttributedLabelDelegate>

@end

@implementation FindCommentToolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        
        
        /**
         UITextField
         */
        BRPlaceholderTextView *bottomTF = [[BRPlaceholderTextView alloc] init];
        bottomTF.layoutManager.allowsNonContiguousLayout = NO;
        bottomTF.delegate = self;
        bottomTF.font = MidFont;
        bottomTF.placeholder = @"优质评论将会被优先展示";
        [bottomTF setPlaceholderColor:CSColorFromRGB(0xDCDCDC)];
        [bottomTF setPlaceholderFont:MidFont];
        [bottomTF setPlaceholderOpacity:0.6];
        bottomTF.textColor = CSBlackColor1;
        bottomTF.layer.borderWidth = 1;
        bottomTF.layer.borderColor = CSColorFromRGB(0xDCDCDC).CGColor;
        bottomTF.layer.cornerRadius = 5;
        bottomTF.layer.masksToBounds = YES;
        bottomTF.backgroundColor = CSClearColor;//CSAlphaColor(23, 24, 36,0.4);
        bottomTF.returnKeyType = UIReturnKeySend;
        [self addSubview:bottomTF];
        self.bottomTF = bottomTF;
        
        
        
        /**
         发送按钮
         */
        
        UIView *sendView = [[UIView alloc]init];
        self.sendView = sendView;
        sendView.layer.cornerRadius = 5;
        sendView.hidden = YES;
        sendView.clipsToBounds = YES;
        [self addSubview:sendView];
        
        UIButton *sendBtn = [[UIButton alloc] init];
        [sendBtn setTitle:@"发表"];
        sendBtn.backgroundColor = CSColorFromRGB(0xF97676);
        sendBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        sendBtn.layer.cornerRadius = 5;
        sendBtn.clipsToBounds = YES;
        [sendBtn addTarget:self action:@selector(sendBtnClick)];
        [self addSubview:sendBtn];
        self.sendBtn = sendBtn;
        
        
        
        [self setConstraints];
        
    }
    return self;
    
}

-(void)setConstraints
{
    //设置约束条件
    
    /**
     UITextField
     */
    [self.bottomTF mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        
        make.left.mas_equalTo(self).offset(12);
        
        make.centerY.mas_equalTo(self);
        
        make.right.mas_equalTo(self.sendBtn.mas_left).offset(-15);
        
        make.height.mas_equalTo(37);
    }];
    
    
    /**
     发送按钮
     */
    [self.sendBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-15);
        make.height.mas_equalTo(37);
        make.width.mas_equalTo(50);
        make.centerY.mas_equalTo(self);
    }];
    [self.sendView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self).offset(-15);
        make.height.mas_equalTo(37);
        make.width.mas_equalTo(50);
        make.centerY.mas_equalTo(self);
    }];
    
}

#pragma mark UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView
{
    
    if (textView == self.bottomTF) {
        if (textView.text.length > 0) {
            self.sendView.hidden = NO;
            self.sendBtn.backgroundColor = CSClearColor;
        }else
        {
            self.sendView.hidden = YES;
            self.sendBtn.backgroundColor = CSColorFromRGB(0xF97676);
        }
    }
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车，即按下return
        
        
        //这里返回NO，就代表return键值失效，即页面上按下return，不会出现换行，如果为yes，则输入页面会换行
        return NO;
    }
    
    if (![text isEmptyString]) {  //不是删除
        
        return YES;
    }
    
    
    
    return YES;
}


#pragma mark - 发送评论
-(void)sendBtnClick
{
    
    if (self.bottomTF.text.length == 0) {
        [MBProgressHUD showText:@"请输入内容~"];
        return;
    }
    
    
}




-(void)drawRect:(CGRect)rect
{
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors =  @[(id)CSColorFromRGB(0xF52C50).CGColor,(id)CSColorFromRGB(0xF95C48).CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1.0, 0);
    gradientLayer.frame = self.sendView.bounds;
    [self.sendView.layer insertSublayer:gradientLayer atIndex:0];
    
    
}


@end
